﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using MySqlX.XDevAPI.Relational;
using Sunny.UI;

namespace RclSystem
{
    public partial class Saleman : UIPage
    {
        public Saleman()
        {
            InitializeComponent();
            uiDataGridView1.AutoGenerateColumns = false;
        }

        private void ListQuery()
        {
            uiDataGridView1.Rows.Clear();
            uiDataGridView1.Refresh();
            DataTable dataTable = MySQLHelper.ExecuteQuery("SELECT * FROM tbl_saleman");
            foreach(DataRow row in dataTable.Rows)
            {
                int index = this.uiDataGridView1.Rows.Add();
                this.uiDataGridView1.Rows[index].Cells[0].Value = row["id"].ToString();
                this.uiDataGridView1.Rows[index].Cells[1].Value = row["name"].ToString();
                this.uiDataGridView1.Rows[index].Cells[2].Value = row["phone"].ToString();
            }
            uiDataGridView1.ClearSelection();
        }

        private void btn_add_Click(object sender, EventArgs e)
        {
            SalemanDialog frm = new SalemanDialog();
            frm.Render();
            frm.ShowDialog();
            if (frm.IsOK)
            {
                Dictionary<string, object> data = new Dictionary<string, object>();
                data.Add("name", frm.SalemanEnity.name);
                data.Add("phone", frm.SalemanEnity.phone);
                int result = MySQLHelper.Insert("tbl_saleman", data);
                ListQuery();
            }

            frm.Dispose();
        }

        private void Saleman_Initialize(object sender, EventArgs e)
        {
            ListQuery();
        }

        private void uiDataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (uiDataGridView1.Columns[e.ColumnIndex].Name == "btn_del" && e.RowIndex >= 0)
            {
                if (this.ShowAskDialog2("确定要删除该数据么?", true))
                {
                    DataGridViewRow vr = uiDataGridView1.Rows[e.RowIndex];
                    int result = MySQLHelper.Delete("tbl_saleman", "id=" + vr.Cells[0].Value.ToString());
                    ListQuery();
                }                    
            }
            else if(uiDataGridView1.Columns[e.ColumnIndex].Name == "btn_edit" && e.RowIndex >= 0)
            {
                DataGridViewRow vr = uiDataGridView1.Rows[e.RowIndex];
                SalemanEntity saleman = new SalemanEntity();
                saleman.id = long.Parse(vr.Cells[0].Value.ToString());
                saleman.name = vr.Cells[1].Value.ToString();
                saleman.phone = vr.Cells[2].Value.ToString();
                SalemanDialog frm = new SalemanDialog();
                frm.Render();
                frm.SalemanEnity = saleman;
                frm.ShowDialog();
                if (frm.IsOK)
                {
                    // 更新数据
                    Dictionary<string, object> data = new Dictionary<string, object>();
                    data.Add("name", frm.SalemanEnity.name);
                    data.Add("phone", frm.SalemanEnity.phone);
                    int result = MySQLHelper.Update("tbl_saleman", data, "id=" + frm.SalemanEnity.id);
                    ListQuery();
                }

                frm.Dispose();
            }
        }
    }
}
